<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 16/4/26
 * Time: 下午2:32
 * Describe:
 */

namespace console\models;


use common\models\OrderExpress;
use common\models\OrderGoods;
use common\models\OrderInfo;
use Yii;
use common\models\tmall1\OrderInfo as TOrderInfo;
use common\models\tmall1\OrderGoods as TOrderGoods;
use yii\base\Exception;
use common\helper\ArrayHelper;

class BestryToDTry extends Object
{
    #public static $ILIKETRY_ID_BASE = 17993;
    public static $ILIKETRY_ID_BASE = 20000000;
    public        $isEnd            = false;
    public        $page_size        = 500;
    public        $begin_id         = 0;
    public        $end_id           = 0;

    public function writeData()
    {
        $start_id = $this->begin_id;
        static::info('导入开始ID:' . $this->begin_id);

        $query = TOrderInfo::find()
                           ->filterWhere(['>', 'uid', $this->begin_id])
                           ->andFilterWhere(['<', 'uid', static::$ILIKETRY_ID_BASE]);
        if ($this->end_id != 0) {
            $query = $query->andFilterWhere(['<', 'uid', $this->end_id]);
        }
        $result = $query->limit($this->page_size)->all();

        if (empty($result)) {
            $this->isEnd = true;
            static::info('bestry.order,最后ID：' . $this->begin_id);
            return 0;
        }
        /** @var TOrderInfo $oldOrder */
        foreach ($result as $oldOrder) {
            $this->begin_id = $oldOrder->uid;
            if ($this->end_id > 0 && $oldOrder->uid > $this->end_id) {
                $this->isEnd = true;
                return;
            }
            $order                       = new OrderInfo();
            $order->order_info_id        = $oldOrder->uid;
            $order->order_sn             = $oldOrder->ordersn;
            $order->bestry_uid           = $oldOrder->member_uid;
            $order->type                 = $oldOrder->type;
            $order->create_time          = date('Y-m-d H:i:s', $oldOrder->addtime);
            $order->status               = $oldOrder->status;
            $order->pay_status           = $oldOrder->pay_result;
            $order->pay_id               = $oldOrder->pay_id;
            $order->pay_name             = $oldOrder->pay_name;
            $order->bank                 = $oldOrder->defaultbank;
            $order->goods_amount         = $oldOrder->goods_amount;
            $order->order_amount         = $oldOrder->order_amount;
            $order->member_money         = $oldOrder->member_money;
            $order->reduce_amount        = $oldOrder->reduce_amount;
            $order->coupon_id            = $oldOrder->coupon_id;
            $order->coupon_code          = $oldOrder->coupon_code;
            $order->coupon_amount        = $oldOrder->coupon_amount;
            $order->ship_amount          = $oldOrder->ship_amount;
            $order->ship_amount_try_coin = 0;
            $order->is_invoice           = $oldOrder->is_invoice;
            $order->remark               = $oldOrder->remark;
            $order->update_time          = date('Y-m-d H:i:s', $oldOrder->modify_time);
            $order->try_coin             = $oldOrder->jfs;
            $order->source               = $oldOrder->origin;
            try {

                $order->save();
            } catch (Exception $e) {
                static::info('导入order失败id:' . $oldOrder->uid . ' ' . $e->getMessage());

            }
            if ($order->hasErrors()) {
                static::info('导入order失败id:' . $oldOrder->uid . ' ' . ArrayHelper::arrayToString($order->getFirstErrors()));
            }

            $express                = new OrderExpress();
            $express->order_info_id = $oldOrder->uid;
            $express->consignee     = mb_substr($oldOrder->consignee, 0, 20, 'utf-8');
            $express->address       = mb_substr($oldOrder->address, 0, 80, 'utf-8');
            $express->province      = $oldOrder->s1;
            $express->city          = $oldOrder->s2;
            $express->county        = $oldOrder->s3;
            $express->zip           = mb_substr($oldOrder->zipcode, 0, 6, 'utf-8');
            $express->telephone     = $oldOrder->tel;
            $express->mobile_phone  = $oldOrder->cell;
            $express->delivery_id   = $oldOrder->delivery_id;
            $express->delivery_time = $oldOrder->delivery_time;
            $express->delivery_code = $oldOrder->delivery_code;

            $express->create_time = date('Y-m-d H:i:s', $oldOrder->addtime);
            try {
                $express->save();
            } catch (Exception $e) {
                static::info('导入order express失败id:' . $oldOrder->uid . ' ' . $e->getMessage());
            }
            if ($express->hasErrors()) {
                static::info('导入order express失败id:' . $oldOrder->uid . ' ' . ArrayHelper::arrayToString($express->getFirstErrors()));
            }
            unset($oldOrder, $express, $order);

        }

        $result = TOrderGoods::find()->filterWhere(['>', 'order_id', $start_id])
                             ->andFilterWhere(['<=', 'order_id', $this->begin_id])
                             ->all();
        /** @var TOrderGoods $tog */
        foreach ($result as $tog) {
            $od                = new OrderGoods();
            $od->order_info_id = $tog->order_id;
            $od->goods_id      = $tog->goods_id;
            $od->product_id    = $tog->product_id;
            $od->bestry_uid    = $tog->member_uid;
            $od->buy_number    = $tog->buy_number;
            $od->buy_price     = $tog->buy_price;
            $od->create_time   = date('Y-m-d H:i:s', $tog->addtime);
            $od->free_gift     = $tog->free_order_id ? 1 : 0;
            $od->commented     = $tog->comment_id > 0 ? 1 : 0;
            $od->activity_id   = $tog->act_id;
            $od->price_type    = $tog->price_type;
            $od->try_coin      = $tog->jfs;
            try {
                $od->save();
            } catch (Exception $e) {
                static::info('导入order goods失败id:' . $tog->uid . ' ' . $e->getMessage());
            }
            if ($od->hasErrors()) {
                static::info('导入order goods 失败:' . $tog->uid . ' ' . ArrayHelper::arrayToString($od->getFirstErrors()));
            }
            unset($od, $tog);

        }
    }


    public function run()
    {
        static::info('开始导入从bestry.order_info');
        if ($this->begin_id === 0 && $this->end_id === 0) {

            #Yii::$app->db->createCommand("ALTER TABLE `d_try`.`order_info` AUTO_INCREMENT=40000000;")->execute();
            $max_id = (int)OrderInfo::find()->where("order_info_id > 0 and order_info_id < " . LiketryToDTry::$ILIKETRY_ID_BASE)->max('order_info_id');

            $max_id -= LiketryToDTry::$ILIKETRY_ID_BASE;
            #$max_id = 17991;
            $this->begin_id = $max_id ? $max_id : 0;
        }

        for ($times = 0; $times < LiketryToDTry::$ILIKETRY_ID_BASE; $times++) {
            $this->writeData();
            if ($this->isEnd) {
                break;
            }
        }
        static::info('结束bestry.order_info导入.');
    }
}